package controller

import (
	"cooke-home/auth-service/model/dto"
	"cooke-home/auth-service/service"
	"cooke-home/common/response"

	"github.com/gin-gonic/gin"
)

type UserController struct {
	userService service.UserService
}

func NewUserController(userService service.UserService) *UserController {
	return &UserController{
		userService: userService,
	}
}

func (uc *UserController) Login(c *gin.Context) {
	var req dto.LoginDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.Username == "" || req.Password == "" {
		response.Error(c, "用户名或密码不能为空", nil)
		return
	}

	res, err := uc.userService.Login(req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "登录成功", res)
}

func (uc *UserController) Refresh(c *gin.Context) {
	var req dto.RefreshTokenDto

	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	if req.RefreshToken == "" {
		response.Error(c, "刷新令牌不能为空", nil)
		return
	}

	res, err := uc.userService.RefreshAccessToken(req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}

	response.Success(c, "令牌刷新成功", res)
}
